<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>表单提交事件</title>
</head>
<body>
<div class="login-form-wrap">
    <h1>表单提交事件</h1>
    <!--onsubmit 表单事件,绑定在form标签上-->
    <form class="login-form" action="#" onsubmit="return fun() " id="a" method="get" autocomplete="off">
        <label>
            <input type="text" id="username" placeholder="Username" value="">
        </label>
        <label>
            <input type="password" id="pwd" name="pwd" placeholder="Pwd" value="">
        </label>
        <input type="submit" value="注册" onsubmit="fun()">
    </form>
</div>
</body>
<script>
    function fun() {
        alert("ok");
        return true;
    }

    document.getElementById("a").onsubmit = function () {
        //通过id获取用户填写的数据
        let username = document.getElementById("username").value;
        let password = document.getElementById("pwd").value;
        //用户名判断
        let reg = /^[a-zA-Z]{4,16}$/;
        if (!reg.test(username)) {
            alert("用户名填写错误,请输入4到16位纯字母");
            return false;
        }


        //密码判断
        let reg1 = /^[\d]{6}$/;
        if (!reg1.test(password)) {
            alert("密码填写错误,请输入6位纯数字");
            return false;
        }
        return true;
    }
</script>
</html>